<?php

class TestPeer extends BaseTestPeer
{
	static public $choices = array(
			null => 'All',
			1 => 'Active',
			0 => 'not Active',
			);
	static public $choices_edit = array(
			1 => 'Active',
			0 => 'not Active',
			);
  static public function getTestStatusChoices()
  {
    return self::$choices;
  }

  static public function getTestStatusChoicesEdit()
  {
    return self::$choices_edit;
  }
  
  public static function getQuestionTest( $courseid, $levelid )
  {
	$testId = TestPeer::getTestId( $levelid );
	$questionIdList = TestPeer::getQuestionIdList($testId);
	return DbFinder::from('Question a')
		->where( 'a.id', 'in', $questionIdList )
		->orderBy('a.id', 'Asc')
		->find()
		;
  }
  //get question id list in test
  public static function getQuestionIdList( $testid )
  {
	$list = DbFinder::from('QuestionTest a')
			->where('a.TestId', $testid)
			->orderBy('a.QuestionId', 'Asc')
			->find()
			;
	$listid = array();		
	if( $list )
	{
		foreach($list as $row)
		{
			$listid[] = $row->getQuestionId();
		}
	}
	else
		$listid[0] = 0;//default
	return $listid;
  }
	//get random test iin course and level
	public static function getTestId( $levelId )
	{
		$count = 0;
		$listTest = DbFinder::from('Test a')
					->where('a.testStatus',1)
					->where('a.levelId', $levelId )
					->orderBy('a.id', 'Asc')
					->find();
		if($listTest)
		{
			$count = count($listTest) - 1;
			$random = rand(0,$count);
			return $listTest[$random]->getId();
		}
		else
			return 0;//default
	}
	//get answer by questionid
	public static function getAnswerByQuestionId( $questionId )
	{
		return DbFinder::from('QuestionAnswer a')
		->where( 'a.idQuestion', $questionId )
		->orderBy('a.id', 'Asc')
		->find()
		;
	}
	//common function
	public static function days_between( $datefrom, $dateto )
	{
		$fromday_start = mktime( 0,0,0, date("m", $datefrom), date("d", $datefrom), date("y", $datefrom) );
		$diff = $dateto = $datefrom;
		$days = ( $dateto - $datefrom )/(60*60*24);
		if( ($datefrom - $fromday_start) + ( $diff%86400 ) > 86400 )
			$days++;
		return $days;
	}
	
	public static function weeks_between( $datefrom, $dateto )
	{
		$day_of_week = date( "w", $datefrom);
		$fromweek_start = $datefrom - ( $day_of_week * 86400 ) - ( $datefrom%86400 );
		$diff_days = TestPeer::days_between( $datefrom, $dateto );
		$diff_weeks = intval( $diff_days/7);
		$seconds_left = ( $diff_days%7 )*86400;
		
		if( ($datefrom - $fromweek_start) + $seconds_left > 604800 )
			$diff_weeks ++;
		return $diff_weeks;
	}
	
	//chk test has any question 
	public static function chk_TestHasQuestion( $testId )
	{
		//0 = not exist, n exist
		$record = DbFinder::from('QuestionTest a')
		->where( 'a.testid', $testId )
		->find()
		;
		if( $record )
			return 1; //yes
		return 0; //no
	}
	
	public static function chk_TrueAnswerByAnswer( $questionId, $answer )
	{
		//0 = false, 1 true
		$record = DbFinder::from('QuestionAnswer a')
		->where( 'a.idQuestion', $questionId )
		->where( 'a.answer', $answer )
		->where( 'a.trueAnswer', 1 )		
		->findOne()
		;
		if( $record )
			return 1; 
		return 0; //no	
	}
	
	//get true answer by quewtion id
	public static function get_TrueAnswerById( $questionId )
	{
		//0 = not exist, n exist
		$record = DbFinder::from('QuestionAnswer a')
		->where( 'a.idQuestion', $questionId )
		->where( 'a.trueAnswer', 1 )
		->findOne()
		;
		if( $record )
			return $record->getAnswer(); 
		return 0; //no
	}
}
